package com.sykj.why.mongo.service.impl;

import com.sykj.why.exception.WhyException;
import com.sykj.why.mongo.dao.BaseDao;
import com.sykj.why.mongo.document.ServiceDocument;
import com.sykj.why.mongo.dto.PageDTO;
import com.sykj.why.mongo.enums.exception.ResultEnum;
import com.sykj.why.mongo.service.BaseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @program: why
 * @description:
 * @author: hqz
 * @create: 2018-09-06 12:22
 **/
@Service("baseService")
public class BaseServiceImpl<T extends ServiceDocument> implements BaseService<T> {
    @Autowired
    private BaseDao<T> baseDao;

    @Override
    public void save(T entity) {
        baseDao.save(entity);
    }

    @Override
    public void saveOrupdate(T entity) {
        baseDao.saveOrupdate(entity);
    }

    @Override
    public void remove(T entity) {
        baseDao.remove(entity);

    }

    @Override
    public void delete(T entity) {
        baseDao.delete(entity);

    }

    @SuppressWarnings("rawtypes")
    @Override
    public List<T> findAll(Class cls) {
        return baseDao.findAll(cls);
    }

    @Override
    public List<T> findAll(Query query, Class<T> t) {
        return baseDao.findAll(query, t);
    }


    @Override
    public List<T> findAll(Class<T> t, boolean removed) {
        return baseDao.findAll(t, removed);
    }

    @SuppressWarnings("rawtypes")
    @Override
    public T get(Class cls, String id, boolean removed) {
        return baseDao.get(cls, id, removed);
    }

    @Override
    public T get(Class cls, String id) {
        return baseDao.get(cls, id);
    }

    @SuppressWarnings("rawtypes")
    @Override
    public T get(String fieldName, Object getValue, Class cls, boolean removed) {
        return baseDao.get(fieldName, getValue, cls, removed);
    }

    @SuppressWarnings("rawtypes")
    @Override
    public T get(String fieldName1, Object getValue1, String fieldName2, Object getValue2, Class cls, boolean removed) {
        return baseDao.get(fieldName1, getValue1, fieldName2, getValue2, cls, removed);
    }

    @SuppressWarnings("rawtypes")
    @Override
    public boolean update(String updatename, Object updatevalue, String id, Class entity) {
        return baseDao.update(updatename, updatevalue, id, entity);
    }

    @SuppressWarnings("rawtypes")
    @Override
    public boolean updateAll(String updatename, Object updatevalue, String updateId, String id, Class entity) {
        return baseDao.updateAll(updatename, updatevalue, updateId, id, entity);
    }

    @SuppressWarnings("rawtypes")
    @Override
    public long count(String fieldName, Object selRemoved, Class entity) {
        return baseDao.count(fieldName, selRemoved, entity);
    }

    @SuppressWarnings("rawtypes")
    @Override
    public long count(String fieldName, Object selRemoved, boolean removed, Class entity) {
        return baseDao.count(fieldName, selRemoved, removed, entity);
    }

    @SuppressWarnings("rawtypes")
    @Override
    public long count(String fieldName1, Object selRemoved1, String fieldName2, Object selRemoved2, boolean removed, Class entity) {
        return baseDao.count(fieldName1, selRemoved1, fieldName2, selRemoved2, removed, entity);
    }

    @Override
    public List<T> findByPage(Query query, int currentPage, int lineSize, Class<T> t) {
        return baseDao.findByPage(query, currentPage, lineSize, t);
    }

    @Override
    public List<T> findDistinctAll(String collection, String key) {
        // TODO Auto-generated method stub
        return baseDao.findDistinctAll(collection, key);
    }

    @Override
    public int getPageSize(int totalCount, int lineSize) {
        return baseDao.getPageSize(totalCount, lineSize);
    }

    @Override
    public long count(Query query, Class<T> entity) {
        // TODO Auto-generated method stub
        return baseDao.count(query, entity);
    }


    /**
     * 验证地区new,考虑到最高权限可以操作该等级及以下等级的数据,废弃session中提取地区的方法
     *
     * @param t     传入实体
     * @param query
     * @param flag  标识,0是查该地区及子地区的数据,1是只查该地区的数据
     * @return
     */
    @Override
    public Query verifyArea(T t, Query query, int flag) throws WhyException {
        //可查询出该地区及该地区以下的数据
        if (flag == 0) {
            if (StringUtils.isNotBlank(t.getVillageId())) {
                query.addCriteria(Criteria.where("villageId").is(t.getVillageId()));
            } else if (!StringUtils.isNotBlank(t.getVillageId()) && StringUtils.isNotBlank(t.getAreaId())) {
                query.addCriteria(Criteria.where("areaId").is(t.getAreaId()));
            } else if (!StringUtils.isNotBlank(t.getVillageId()) && !StringUtils.isNotBlank(t.getAreaId()) && StringUtils.isNotBlank(t.getCityId())) {
                query.addCriteria(Criteria.where("cityId").is(t.getCityId()));
            } else if (!StringUtils.isNotBlank(t.getVillageId()) && !StringUtils.isNotBlank(t.getAreaId()) && !StringUtils.isNotBlank(t.getCityId()) && StringUtils.isNotBlank(t.getProvinceId())) {
                query.addCriteria(Criteria.where("provinceId").is(t.getProvinceId()));
            } else {
                return query;
            }
            //只能查出该地区的数据
        } else if (flag == 1) {
            query.addCriteria(Criteria.where("provinceId").is(t.getProvinceId()));
            query.addCriteria(Criteria.where("cityId").is(t.getCityId()));
            query.addCriteria(Criteria.where("areaId").is(t.getAreaId()));
            query.addCriteria(Criteria.where("villageId").is(t.getVillageId()));
        }
        return query;
    }


    @Override
    public Query verifyArea4Pagination(Query query, PageDTO pageDTO) {
        Criteria criteria = Criteria.where("level").is(pageDTO.getLevel());
        if (StringUtils.isNotBlank(pageDTO.getVillageId())) {
            criteria.and("villageId").is(pageDTO.getVillageId());
        } else if (!StringUtils.isNotBlank(pageDTO.getVillageId()) && StringUtils.isNotBlank(pageDTO.getAreaId())) {
            criteria.and("areaId").is(pageDTO.getAreaId());
        } else if (!StringUtils.isNotBlank(pageDTO.getVillageId()) && !StringUtils.isNotBlank(pageDTO.getAreaId()) && StringUtils.isNotBlank(pageDTO.getCityId())) {
            criteria.and("cityId").is(pageDTO.getCityId());
        } else if (!StringUtils.isNotBlank(pageDTO.getVillageId()) && !StringUtils.isNotBlank(pageDTO.getAreaId()) && !StringUtils.isNotBlank(pageDTO.getCityId()) && StringUtils.isNotBlank(pageDTO.getProvinceId())) {
            criteria.and("provinceId").is(pageDTO.getProvinceId());
        } else {
            return query;
        }
        return query.addCriteria(criteria);
    }


    /**
     * 设置地区
     *
     * @param t
     * @return
     */
    @Override
    public T setAddress(T t) {
        t.setProvinceId(t.getProvinceId());
        t.setCityId(t.getCityId());
        t.setAreaId(t.getAreaId());
        t.setVillageId(t.getVillageId());
        t.setLevel(t.getLevel());
        return t;
    }

    /**
     * 验证是否拿到机构Id
     *
     * @param orgId
     * @return
     */
    /*@Override
    public void verifyOrgId(String orgId) {
        if (orgId == null) {
            throw new WhyException(ResultEnum.ORGANIZATIONID_NULL);
        }
    }*/

}
